<?php
class image
{
	private $dst;//目标图像
	private $type;//image type
	
	const MAX_THUMB_WIDTH = 260;
	const MAX_THUMB_HEIGHT = 400;
	 
	public function __construct() {
		
	}
	
	
	public static function  createImage($input) {
		if(is_file($input)) {
			return image::createImageFromFile($input);
		} else if (is_string($input)) {
			return image::createImageFromString($input);
		} else if (image::isGDimageResource($input)) {
			return $input;
		}
	}
	/**
	 * 创建不同类型的图像
	 *
	 * @param is_string $file_name
	 * @return resource
	 */
	
	public static function createImageFromFile($file_name) {
		if(!is_file($file_name) || !is_readable($file_name)) {
			return die("不能打开这个图片:$file_name");
		}
		//获取图片信息
		$type = image::getImageType($file_name);
		
		switch ($type) {
			case IMAGETYPE_JPEG:
				return imagecreatefromjpeg($file_name);
				break;
			case IMAGETYPE_GIF:
				return imagecreatefromgif($file_name);
				break;
			case IMAGETYPE_PNG:
				return imagecreatefrompng($file_name);
				break;
			case IMAGETYPE_WBMP:
				return imagecreatefromwbmp($file_name);
				break;
			case IMAGETYPE_XBM:
				return imagecreatefromxbm($file_name);
				break;
			default:
				throw new Exception('不支持这种图片类型');
		}
	}
	
	public static function createImageFromString($file_name) {
		if(!is_string($file_name) || empty($file_name)) {
			die('没有图片');
		}
		
		$img = imagecreatefromstring($file_name);
		
		if($img === false) {
			throw new Exception('创建图片标志符失败');
		}
		
		return $img;
	}
	
	public static function isGDimageResource($value) {
		if(is_resource($value) && get_resource_type($value) == 'gd') {
			return true;
		}
		return false;
	}

	public function save($path,$type) {
		if(!image::isGDimageResource($this->dst)) {
			throw new Exception("$this->dst不是一个资源");
		}
		
		switch ($type) {
			case IMAGETYPE_GIF:
				$res = imagegif($this->dst,$path);
				break;
			case IMAGETYPE_JPEG:
				$res = imagejpeg($this->dst,$path);
				break;
			case IMAGETYPE_PNG:
				$res = imagepng($this->dst,$path);
				break;
			case IMAGETYPE_WBMP:
				$res = imagewbmp($this->dst,$path);
				break;
			default:
				throw new Exception("php不支持$this->type类型的图片");		
		}
		return true;
	}
	/**
	 *  图片的类型
	 *
	 * @param 源图像 $img
	 * @return 图片类型
	 */
	public static function getImageType($img) {
		list($w,$h,$type) = getimagesize($img);
		return $type;
	}
	
	
	/**
	 * 缩略图的生成
	 * 
	 */
	public function createThumbnail($input) {
		$path = image::handlePaht($input);
		
		$src = image::createImage($path);
		
		$width = imagesx($src);
		$height = imagesy($src);
		
		//设置缩略图的宽度和高度
		$ret = image::setWidthAndHeight($width,$height);
		
		$this->dst = imagecreatetruecolor($ret[0],$ret[1]);//创建输出图像
		imagecopyresampled($this->dst,$src,0,0,0,0,$ret[0],$ret[1],$width,$height);//拷贝图像
		
		$type = image::getImageType($path);
		image::save($path,$type);
	}
	
	/**
	 * 根据高度和宽度缩略
	 *
	 * @param int $width
	 * @param int $height
	 */
	public static function setWidthAndHeight($width,$height) {
		$ratio = $width / $height;
		$maxWidth = min($width,self::MAX_THUMB_WIDTH);
		if($maxWidth == 0)
			$maxWidth = $width;
			
		$maxHeight = min($height,self::MAX_THUMB_HEIGHT);
		if($maxHeight == 0) 
			$maxHeight = $height;
		
		$ret[0] = $maxWidth;
		$ret[1] = $ret[0] / $ratio;
			
		if($ret[1] > $maxHeight) {
			$ret[1] = $maxHeight;
			$ret[0] = $ret[1] * $ratio;
		}
		
		if ($width == $ret[0] && $height == $ret[1]) {
			
		}
		
		return $ret;
	}
	/**
	 *  图片的路径处理
	 * 
	 */
	private function handlePaht($url) {
		$url = str_replace('\'','',$url);
		return str_replace('/beauty/user/','',USERPATH).$url;
	}
	
}
?>